﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//简单工厂模式

namespace Question2
{
    class Program
    {
        static void Main(string[] args)
        {
            SimpleFactory sf = new SimpleFactory();
            Random random = new Random(Guid.NewGuid().GetHashCode());
            double sum = 0;
            for(int i = 0; i < 10; i++)
            {
                int shape = random.Next(0, 3);
                sum += sf.createShape(shape).GetArea();
            }
            Console.WriteLine("总面积为：" + sum);
        }
    }

    public class SimpleFactory
    {
        public Shape createShape(int shape)
        {
            Random random = new Random(Guid.NewGuid().GetHashCode());
            switch (shape)
            {
                case 0:
                    {
                        int side = random.Next(1, 11);
                        Console.WriteLine("生成了一个正方形：" + side);
                        return new Square(side);
                    }
                case 1:
                    {
                        int length = random.Next(1, 11);
                        int width = random.Next(1, 11);
                        Console.WriteLine("生成了一个长方形：" + length + "，" + width);
                        return new Rectangle(length, width);
                    }
                case 2:
                    {
                        int a=0, b=0, c = 0;
                        while(!((a + b > c) && (b + c > a) && (a + c > b)))
                        {
                            a = random.Next(1, 11);
                            b = random.Next(1, 11);
                            c = random.Next(1, 11);
                        }
                        Console.WriteLine("生成了一个三角形：" + a + "，" + b+"，"+c);
                        return new Triangle(a, b, c);
                    }
                default:  Console.WriteLine("Please enter the right shape."); return null; 
            }
        }
    }

    public abstract class Shape
    {
        public abstract double GetArea();
    }

    public class Rectangle : Shape
    {
        public Rectangle(double length,double width)
        {
            Length = length;
            Width = width;
        }
        public double Length { get; set; }
        public double Width { get; set; }
        override public double GetArea()
        {
            return Length * Width;
        }
    }

    public class Square : Rectangle
    {
        public Square(double side) : base(side, side) { }
    }

    public class Triangle : Shape
    {
        public Triangle(double a,double b,double c)
        {
            A = a;
            B = b;
            C = c;
        }
        public double A { get; set; }
        public double B { get; set; }
        public double C { get; set; }
        public override double GetArea()
        {
            double p = (A + B + C) / 2;
            return Math.Sqrt(p * (p - A) * (p - B) * (p - C));
        }
    }
}
